package cn.xz.zysjz.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.google.gson.Gson;

import java.util.ArrayList;

import cn.xz.zysjz.R;
import cn.xz.zysjz.adapter.IndexListAdapter;
import cn.xz.zysjz.bean.ActivityListBean;
import cn.xz.zysjz.bean.AppData;
import cn.xz.zysjz.bean.CommonLib;
import cn.xz.zysjz.bean.GetBean;
import cn.xz.zysjz.bean.HomeListBean;
import cn.xz.zysjz.bean.IndexBean;
import cn.xz.zysjz.bean.PFBean;
import cn.xz.zysjz.bean.PLBean;
import cn.xz.zysjz.databinding.ActivityIndexBinding;
import cn.xz.zysjz.databinding.ActivityWebBinding;
import cn.xz.zysjz.dialog.ShowDialog;
import cn.xz.zysjz.utils.AppHelper;
import cn.xz.zysjz.utils.GetDataUtils;
import cn.xz.zysjz.utils.SessionUtils;
import cn.xz.zysjz.utils.ToastUtils;

/**
 * 详情页*/
public class IndexActivity extends AppCompatActivity {

    //绑定xml布局文件-也就是手机上看到的界面
    ActivityIndexBinding mBinding;

    private HomeListBean mData;

    private boolean mPfBean = true; //是否评分过


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_index);
        if (Build.VERSION.SDK_INT >= 21) {
            this.getWindow()
                    .setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        }
        //获取对应的疫苗数据
        mData = (HomeListBean) getIntent().getSerializableExtra(CommonLib.JUMP_TYPE);
        //绑定该用户id
        if (CommonLib.nowUserBean != null){
            mData.setScUser(CommonLib.nowUserBean.getName().toString());
        }
        //管理员显示添加按钮
        mBinding.llcAdd.setVisibility(View.VISIBLE);
        mBinding.llcP1.setVisibility(View.GONE);
        mBinding.llcAdd.setVisibility(View.GONE);
        mBinding.llcP1.setVisibility(View.VISIBLE);
        if (CommonLib.isAdmin){
            mBinding.llcAdd.setVisibility(View.VISIBLE);
            mBinding.llcP1.setVisibility(View.GONE);
        }
        //点击事件
        initEvent();
        //数据相关
        initData();
    }

    private void initData(){
        //设置标题
        mBinding.tvTitle.setText(mData.getMsg());
        //说明
        mBinding.tvMsg.setText(mData.getMsg());
        //图片
        AppHelper.setLocalPic(mBinding.ivPic,mData.getUrl());
        //是否免费
        String mS = "免费，不会产生费用";
        if (!mData.getFree()){
            mS = "需要付费，请合理消费";
        }
        mBinding.tvFree.setText(mS);
        //说明
        mBinding.tvSm.setText(GetDataUtils.getSM(mData.getNum()));
        isLike();
        onPf();
        getPL();
    }


    private void onPf(){
        mPfBean = true;
        mBinding.ivPff.setImageDrawable(AppHelper.getDrawable(R.mipmap.icon_pff));
        mBinding.tvPing.setTextColor(Color.parseColor("#d81e06"));
        //热度
        mBinding.tvRe.setText("热度: " + mData.getGet() );
        //是否评过分
        if (!GetDataUtils.setPf(mData.getNum())){
            //没评过分
            mPfBean = false;
            mBinding.ivPff.setImageDrawable(AppHelper.getDrawable(R.mipmap.icon_pff_1));
            mBinding.tvPing.setTextColor(Color.parseColor("#8a8a8a"));
        }
    }

    //是否收藏
    private void isLike(){
        //是否收藏
        ArrayList<HomeListBean> list = SessionUtils.getList(CommonLib.SC_LIST, HomeListBean.class);
        mBinding.ivLike.setImageDrawable(AppHelper.getDrawable(R.mipmap.icon_scl));
        mBinding.tvZan.setTextColor(Color.parseColor("#8a8a8a"));
        for (HomeListBean homeListBea : list){
            if (homeListBea.getNum() == mData.getNum()){
                mBinding.ivLike.setImageDrawable(AppHelper.getDrawable(R.mipmap.icon_scl_1));
                mBinding.tvZan.setTextColor(Color.parseColor("#d81e06"));
            }
        }
    }

    private void initEvent(){
        //退出
        mBinding.viewBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        //添加
        mBinding.llcAdd.setOnClickListener(v->{
            //AppHelper.toJump(AddYActivity.class);
            ShowDialog.show3(IndexActivity.this, new ShowDialog.OnShowClickStr() {
                @Override
                public void click(String s) {
                    mBinding.llcAdd.post(new Runnable() {
                        @Override
                        public void run() {
                            //说明
                            mBinding.tvSm.setText(s);
                            GetDataUtils.upData(mData.getNum(),s);
                        }
                    });
                }
            });
        });
        //评分
        mBinding.llcP.setOnClickListener(v->{
            if (mPfBean){
                ToastUtils.show("已经评过分了");
                return;
            }
            ShowDialog.show4(this, new ShowDialog.OnShowClickPf() {
                @Override
                public void click(int i) {
                    ArrayList<PFBean> list = SessionUtils.getList(CommonLib.PF_LIST, PFBean.class);
                    PFBean pfBean = new PFBean();
                    pfBean.setNum(mData.getNum());
                    pfBean.setPrice(i);
                    list.add(pfBean);
                    SessionUtils.commitList(CommonLib.PF_LIST,list);
                    ToastUtils.show("评分成功");


                    mPfBean = true;
                    mBinding.ivPff.setImageDrawable(AppHelper.getDrawable(R.mipmap.icon_pff));
                    mBinding.tvPing.setTextColor(Color.parseColor("#d81e06"));
                    //热度
                    mBinding.tvRe.setText("热度: " + (mData.getGet() + GetDataUtils.getPf(mData.getNum())) );
                }
            });
        });
        //收藏
        mBinding.llcShouc.setOnClickListener(v->{
            //获取收藏列表
            ArrayList<HomeListBean> list = SessionUtils.getList(CommonLib.SC_LIST, HomeListBean.class);
            //看下有没有收藏
            int sc = -1;
            for (int i = 0; i < list.size(); i++){
                if (list.get(i).getNum() == mData.getNum()){
                    sc = i;
                }
            }
            if (sc != -1){
                //收藏了，取消收藏
                list.remove(sc);
            }else {
                //没有收藏，开始收藏
                list.add(mData);
            }
            SessionUtils.commitList(CommonLib.SC_LIST,list);
            isLike();
        });
        //评论
        mBinding.tvPl.setOnClickListener(v->{
            ShowDialog.show3(IndexActivity.this, new ShowDialog.OnShowClickStr() {
                @Override
                public void click(String s) {
                    GetDataUtils.inputPL(new PLBean(mData.getNum(),CommonLib.nowUserBean.getName(),AppHelper.getTimer(),s));
                    getPL();
                }
            });
        });
        //管理员删除评论
        mBinding.llcPlll.setOnClickListener(v->{
            if (CommonLib.isAdmin){
                ShowDialog.show2("请输入要删除评论的关键字", IndexActivity.this, new ShowDialog.OnShowClickStr() {
                    @Override
                    public void click(String s) {
                        GetDataUtils.delPL(mData.getNum(),s);
                        getPL();
                    }
                });
            }
        });
    }

    //获取评论
    private void getPL(){
        ArrayList<PLBean> pl = GetDataUtils.getPL(mData.getNum());
        if (pl.size() > 0){
            mBinding.llcPlll.removeAllViews();
            for (PLBean s : pl) {
                TextView textView = new TextView(IndexActivity.this);
                textView.setId(textView.getImeActionId());
                textView.setText(s.getName() + ": " + s.getTxt() + "-" + s.getTime());
                textView.setTextColor(0xff999faa);
                textView.setTextSize(16);
                mBinding.llcPlll.addView(textView);
            }
        }
    }
}